package com.rede.didiok.admin.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rede.didiok.admin.utils.FileUtil;
import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.dto.file.FileDto;
import com.rede.didiok.common.dto.user.UserDto;
import com.rede.didiok.common.enums.FilePriorityEnum;
import com.rede.didiok.common.feign.FileFeignClient;
import com.rede.didiok.common.feign.UserFeignClient;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;

/**
 * 文件服务实现类【上传需调用本地文件服务、七牛云文件服务、Minio文件服务】
 *
 * @author 陌溪
 * @since 2018-09-17
 */
@Slf4j
@Service("fileSupport")
public class FileSupport {

    @Resource
    FileFeignClient fileFeignClient;
    @Resource
    UserFeignClient userFeignClient;
    @Resource
    FileUtil fileUtil;

    public IPage<FileDto> getFileList(FileDto fileDto) {
        // 获取系统配置文件
        SystemConfigDto systemConfig = fileUtil.getSystemConfig();

        R result = fileFeignClient.getFilePage(fileDto);

        IPage<FileDto> pageIPage = result.getData(new TypeReference<IPage<FileDto>>(){});
        List<FileDto> fileList = pageIPage.getRecords();

        Map<String, UserDto> userMap = new HashMap<>();
        if (fileDto.isShowUser()) {
            List<UserDto> userList = fileDto.getUserList();
            if(null == userList){
                List<String> userUidList = new ArrayList<>();
                fileList.forEach(item -> {
                    userUidList.add(item.getCreateUserUid());
                });
                if (userUidList.size() > 0) {
                    UserDto userDto = new UserDto();
                    userDto.setUserUidList(userUidList);
                    userDto.setCurrentPage(1L);
                    userDto.setPageSize(100L);
                    userList = userFeignClient.getUserListByPage(userDto);
                }
            }
            userList.forEach(item -> {
                userMap.put(item.getUid(), item);
            });
        }

        // 设置图片展示的优先级
        for (FileDto file : fileList) {
            file.setUser(userMap.get(file.getCreateUserUid()));
            // 按优先级设置图片
            setFilePriority(file, systemConfig);
        }
        pageIPage.setRecords(fileList);


        return pageIPage;
    }

    /**
     * 根据图片优先级，设置图片
     *
     * @param file
     * @param systemConfig
     */
    private void setFilePriority(FileDto file, SystemConfigDto systemConfig) {
        if (file == null || systemConfig == null) {
            return;
        }
        String picturePriority = systemConfig.getPicturePriority();
        if (FilePriorityEnum.QI_NIU.equals(picturePriority)) {
            file.setPictureUrl(systemConfig.getQiNiuPictureBaseUrl() + file.getQiNiuUrl());
        } else if (FilePriorityEnum.MINIO.equals(picturePriority)) {
            file.setPictureUrl(systemConfig.getMinioPictureBaseUrl() + file.getMinioUrl());
        } else if (FilePriorityEnum.LOCAL.equals(picturePriority)) {
            file.setPictureUrl(systemConfig.getLocalPictureBaseUrl() + file.getPicUrl());
        } else if (FilePriorityEnum.ALIOSS.equals(picturePriority)) {
            file.setPictureUrl(systemConfig.getAliossPictureBaseUrl() + file.getAliOssUrl());
        }
    }

    /**
     * 批量删除素材列表
     * @param fileUidList
     * @return
     */
    public String batchDeleteFile(List<String> fileUidList) {
        if (fileUidList.size() == 0 || fileUidList.size() > 100) {
            return ResultUtil.errorWithMessage("参数异常");
        }
        // 获取系统配置文件
        SystemConfigDto systemConfig = fileUtil.getSystemConfig();
        systemConfig.setFileUidList(fileUidList);
        return fileFeignClient.batchDeleteFileSystemConfig(systemConfig);

    }

}


